package com.online.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.online.entity.ApiResult;
import com.online.entity.Student;
import com.online.service.StudentService;
import com.online.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 学生
 */
@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/students/{page}/{size}")
    public ApiResult findAll(@PathVariable Integer page,@PathVariable Integer size){
        Page<Student> studentPage=new Page<>(page,size);
        IPage<Student> res=studentService.findAll(studentPage);
        return ApiResultHandler.buildApiResult(200,"分页查询所有学生",res);
    }

    @GetMapping("/student/{studentId}")
    public ApiResult findById(@PathVariable("studentId") Integer studentId){
       Student res= studentService.findById(studentId);
       if (res!=null){
           return  ApiResultHandler.buildApiResult(200,"请求成功",res);

       }else{
           return ApiResultHandler.buildApiResult(404,"查询的用户不存在",null);
       }
    }

    @PutMapping("/studentPWD")
    public ApiResult updatePwd(@RequestBody Student student){
        studentService.updatePwd(student);
        return ApiResultHandler.buildApiResult(200,"密码更新成功",null);
    }

    @PutMapping("/student")
    public ApiResult update(@RequestBody Student student){
        int res=studentService.update(student);
        if (res!=0){
            return ApiResultHandler.buildApiResult(200,"更新成功",res);
        }
        return ApiResultHandler.buildApiResult(400,"更新失败",res);
    }
    @DeleteMapping("/student/{studentId}")
    public ApiResult deleteById(@PathVariable("studentId") Integer studentId){

        return ApiResultHandler.buildApiResult(200,"删除成功",studentService.deleteById(studentId));
    }
    @PostMapping("/student")
    public ApiResult add(@RequestBody Student student){
        int res=studentService.add(student);
        if (res==1){
            return ApiResultHandler.buildApiResult(200,"添加成功",res);
        }else {
            return ApiResultHandler.buildApiResult(400,"添加失败",null);
        }
    }
}
